package meiyunting.shiwuzhaoling.controller;

import meiyunting.shiwuzhaoling.model.User;
import meiyunting.shiwuzhaoling.model.UserRole;
import meiyunting.shiwuzhaoling.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private final UserService userService;

    public AdminController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    public String adminDashboard() {
        return "admin/dashboard";
    }

    @GetMapping("/users")
    public String listUsers(Model model) {
        List<User> users = userService.findAllUsers();
        model.addAttribute("users", users);
        return "admin/users";
    }

    @GetMapping("/users/{id}")
    public String viewUser(@PathVariable Long id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "admin/user-detail";
    }

    @GetMapping("/users/{id}/edit")
    public String editUserForm(@PathVariable Long id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        model.addAttribute("roles", UserRole.values());
        return "admin/user-edit";
    }

    @PostMapping("/users/{id}/edit")
    public String updateUser(@PathVariable Long id, 
                           @RequestParam String username,
                           @RequestParam String email,
                           @RequestParam UserRole role,
                           RedirectAttributes redirectAttributes) {
        userService.updateUser(id, username, email, role);
        redirectAttributes.addFlashAttribute("successMessage", "用户信息已更新");
        return "redirect:/admin/users/" + id;
    }

    @PostMapping("/users/{id}/role")
    public String updateUserRole(@PathVariable Long id, 
                               @RequestParam UserRole role,
                               RedirectAttributes redirectAttributes) {
        userService.updateUserRole(id, role);
        redirectAttributes.addFlashAttribute("successMessage", "用户角色已更新");
        return "redirect:/admin/users";
    }

    @PostMapping("/users/{id}/delete")
    public String deleteUser(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        try {
            // 记录删除操作
            System.out.println("尝试删除用户，ID: " + id);
            
            // 获取用户信息用于日志记录
            User user = userService.findById(id);
            System.out.println("删除的用户信息 - 用户名: " + user.getUsername() + ", 邮箱: " + user.getEmail());
            
            // 执行删除操作
            userService.deleteUser(id);
            
            System.out.println("用户删除成功，ID: " + id);
            redirectAttributes.addFlashAttribute("successMessage", "用户已成功删除");
            return "redirect:/admin/users";
        } catch (Exception e) {
            System.err.println("删除用户失败，ID: " + id + ", 错误: " + e.getMessage());
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("errorMessage", "删除用户失败: " + e.getMessage());
            return "redirect:/admin/users";
        }
    }

    @GetMapping("/direct-access")
    public String directAccess(Model model) {
        // 获取所有用户
        List<User> users = userService.findAllUsers();
        model.addAttribute("users", users);
        return "admin/users";
    }
} 